import globalConst from "../globalConst";
var global = globalConst.GlobalConst;

import utilfuncs from "./utilfuncs";
var funcs = utilfuncs.UtilFuncs;

var CoordTRFUtil = {

}

CoordTRFUtil.world2Screen = function (pt, mapInfo) {
    var scaling = mapInfo.getScaling();
    if (mapInfo.getScaling() > global.MaxDisScaling) {
        scaling = global.MaxDisScaling;
    }
    else if (scaling < global.MinDisScaling) {
        scaling = global.MinDisScaling;
    }

    var projectScale = mapInfo.getProjectScale();
    let mapHgt = mapInfo.getMapWindowRect().top;
    let modelTransX = mapInfo.viewPortRect.left;
    let modelTransY = mapInfo.viewPortRect.bottom;

    //模型坐标转换成投影坐标
    let xp = (pt.x - modelTransX) * scaling;
    let yp = (pt.y - modelTransY) * scaling;

    let scale = scaling / projectScale;

    //投影坐标转换成屏幕坐标
    let x = (pt.x - modelTransX) * scale;
    let y = mapHgt - (pt.y - modelTransY) * scale;
    return { x, y };
}

CoordTRFUtil.screen2world = function (pt, mapInfo) {
    var scaling = mapInfo.getScaling();
    if (mapInfo.getScaling() > global.MaxDisScaling) {
        scaling = global.MaxDisScaling;
    }
    else if (scaling < global.MinDisScaling) {
        scaling = global.MinDisScaling;
    }

    var projectScale = mapInfo.getProjectScale();
    let mapHgt = mapInfo.getMapWindowRect().top;

    let modelTransX = mapInfo.viewPortRect.left;
    let modelTransY = mapInfo.viewPortRect.bottom;

    let scale = projectScale / scaling;
    let x = pt.x * scale + modelTransX;
    let y = (mapHgt - pt.y) * scale + modelTransY;
    return { x, y };
}

CoordTRFUtil.transLocalPt2World = function (pt, radAngle, transformInfo) {
    let scalex = transformInfo.sx;
    let scaley = transformInfo.sy;

    let rotRad = radAngle + transformInfo.rotRad;
    let x = pt.x * scalex + transformInfo.orinPt.x;
    let y = pt.y * scaley + transformInfo.orinPt.y;
    let worldPt = funcs.rotate({ x, y }, transformInfo.orinPt, rotRad);
    return worldPt;
}

CoordTRFUtil.local2Screen = function (pt, mapInfo) {
    var scaling = mapInfo.getScaling();
    if (mapInfo.getScaling() > global.MaxDisScaling) {
        scaling = global.MaxDisScaling;
    }
    else if (scaling < global.MinDisScaling) {
        scaling = global.MinDisScaling;
    }

    var projectScale = mapInfo.getProjectScale();
    let scale = scaling / projectScale;
    let x = pt.x * scale;
    let y = -pt.y * scale;
    return { x, y };
}

CoordTRFUtil.transLocalRect2World = function (rect, radAngle, transformInfo) {
    let ptList = [];
    let ltPt = rect.ltPt();
    let ltwpt = this.transLocalPt2World(ltPt, radAngle, transformInfo);
    ptList.push(ltwpt);

    let rtPt = rect.rtPt();
    let rtwpt = this.transLocalPt2World(rtPt, radAngle, transformInfo);
    ptList.push(rtwpt);

    let rbPt = rect.rbPt();
    let rbwpt = this.transLocalPt2World(rbPt, radAngle, transformInfo);
    ptList.push(rbwpt);

    let lbPt = rect.lbPt();
    let lbwpt = this.transLocalPt2World(lbPt, radAngle, transformInfo);
    ptList.push(lbwpt);
    return funcs.calPtListZone(ptList);
}

CoordTRFUtil.transWorldRect2Screen = function (rect,mapInfo) {
    let ptList = [];
    let ltPt = rect.ltPt();
    let ltSpt = this.world2Screen(ltPt,mapInfo);
    ptList.push(ltSpt);

    let rtPt = rect.rtPt();
    let rtSpt = this.world2Screen(rtPt,mapInfo);
    ptList.push(rtSpt);

    let rbPt = rect.rbPt();
    let rbspt = this.world2Screen(rbPt,mapInfo);
    ptList.push(rbspt);

    let lbPt = rect.lbPt();
    let lbSpt = this.world2Screen(lbPt,mapInfo);
    ptList.push(lbSpt);
    return funcs.calPtListZone(ptList);
}

export { CoordTRFUtil }